﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using Kugar.Core.BaseStruct;
using Kugar.Core.ExtMethod;
using Kugar.Core.Web;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace Kugar.UI.Web.AdminLTE.PageControls.SystemComponent
{
    public class PageCommon_ServerMsg:ControlBase
    {
        public PageCommon_ServerMsg(ViewContext viewContext) : base(viewContext)
        {
        }

        public HtmlString RenderNoWebUIJS(bool forceRenderJQ=false)
        {
            var lst = (List<VM_MVCMsgBoxItem>)ViewContext.ViewBag.MsgData_Temp;

            if (forceRenderJQ)
            {
                ViewContext.Writer.WriteLine($"<script src=\"{AdminLTEUI.LayoutPath.ResourceBaseUrl}/Content/lib/jquery/jquery.min.js\" charset=\"utf-8\"></script>");
            }

            if (lst.HasData())
            {
                var n1 = $"scriptServerMsg_{RandomEx.Next()}";

                if (!forceRenderJQ)
                {
                    ViewContext.Writer.WriteLine($"<script src=\"{AdminLTEUI.LayoutPath.ResourceBaseUrl}/Content/lib/jquery/jquery.min.js\" charset=\"utf-8\"></script>");
                    
                }

                ViewContext.Writer.WriteLine($"<script src=\"{AdminLTEUI.LayoutPath.ResourceBaseUrl}/Content/lib/layui/layui.js\" charset=\"utf-8\"></script>");

                ViewContext.Writer.WriteLine($"<script id=\"{n1}\" type=\"text/javascript\">");

                ViewContext.Writer.WriteLine($@"var WebUIJS={{}};
                            WebUIJS.GoTo=function(url){{window.location.href=url;}};
                                ");

                ViewContext.Writer.WriteLine("$(document).ready(function(){ layui.use('layer', function(){ ");

                foreach (var item in lst)
                {
                    if (!string.IsNullOrWhiteSpace(item.Message))
                    {
                        ViewContext.Writer.WriteLine($"layui.layer.alert('{HttpUtility.HtmlEncode(item.Message)}',function(index){{ layer.close(index); ");
                    }

                    if (!string.IsNullOrWhiteSpace(item.JavsScript))
                    {
                        var n = $"severmsg_{RandomEx.Next()}";
                        ViewContext.Writer.WriteLine($"function {n} (){{");
                        ViewContext.Writer.WriteLine(item.JavsScript);
                        ViewContext.Writer.WriteLine("}");
                        ViewContext.Writer.WriteLine($"{n}.call($('#{n1}'));");
                    }

                    ViewContext.Writer.WriteLine("});");

                }
                ViewContext.Writer.WriteLine("}) });");


                ViewContext.Writer.WriteLine("</script>");

            }

            return HtmlString.Empty;
        }

        public override HtmlString Render()
        {
            var lst = (List<VM_MVCMsgBoxItem>)ViewContext.ViewBag.MsgData_Temp;

            if (lst.HasData())
            {
                var n1 = $"scriptServerMsg_{RandomEx.Next()}";

                ViewContext.Writer.WriteLine($"<script id=\"{n1}\" type=\"text/javascript\">");

                ViewContext.Writer.WriteLine("$(document).ready(function(){ layui.use('layer', function(){ ");

                foreach (var item in lst)
                {
                    if (!string.IsNullOrWhiteSpace(item.Message))
                    {
                        ViewContext.Writer.WriteLine($"WebUIJS.Alert('{HttpUtility.HtmlEncode(item.Message)}',function(index){{ ");
                    }

                    if (!string.IsNullOrWhiteSpace(item.JavsScript))
                    {
                        var n = $"severmsg_{RandomEx.Next()}";
                        ViewContext.Writer.WriteLine($"function {n} (){{");
                        ViewContext.Writer.WriteLine(item.JavsScript);
                        ViewContext.Writer.WriteLine("}");
                        ViewContext.Writer.WriteLine($"{n}.call($('#{n1}'));");
                    }

                    ViewContext.Writer.WriteLine("})");

                }
                ViewContext.Writer.WriteLine("}) });");


                ViewContext.Writer.WriteLine("</script>");

            }

            return HtmlString.Empty;
        }
    }
}
